﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="DiagonalMatrixExtensions.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential
{
    #region Usings

    using System;

    using ExulLibrary.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Exceptions;

    #endregion

    /// <summary>
    ///     Provides extension methods for <see cref="DiagonalMatrix{T}"/>.
    /// </summary>
    public static class DiagonalMatrixExtensions
    {
        #region Methods

        #region public

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign<T>(this DiagonalMatrix<T> target, DiagonalMatrix<T> source)
        {
            #if SAFE

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var size = Math.Min(target.RowsCount, source.RowsCount);
            Array.Copy(source.Values, target.Values, size);
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<bool> target, DiagonalMatrix<bool> source)
        {
            Assign(target, source, sizeof(bool));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<byte> target, DiagonalMatrix<byte> source)
        {
            Assign(target, source, sizeof(byte));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this DiagonalMatrix<sbyte> target, DiagonalMatrix<sbyte> source)
        {
            Assign(target, source, sizeof(sbyte));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<char> target, DiagonalMatrix<char> source)
        {
            Assign(target, source, sizeof(char));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<float> target, DiagonalMatrix<float> source)
        {
            Assign(target, source, sizeof(float));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<double> target, DiagonalMatrix<double> source)
        {
            Assign(target, source, sizeof(double));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<short> target, DiagonalMatrix<short> source)
        {
            Assign(target, source, sizeof(short));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this DiagonalMatrix<ushort> target, DiagonalMatrix<ushort> source)
        {
            Assign(target, source, sizeof(ushort));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<int> target, DiagonalMatrix<int> source)
        {
            Assign(target, source, sizeof(int));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this DiagonalMatrix<uint> target, DiagonalMatrix<uint> source)
        {
            Assign(target, source, sizeof(uint));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        public static void Assign(this DiagonalMatrix<long> target, DiagonalMatrix<long> source)
        {
            Assign(target, source, sizeof(long));
        }

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The size of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        /// </remarks>
        [CLSCompliant(false)]
        public static void Assign(this DiagonalMatrix<ulong> target, DiagonalMatrix<ulong> source)
        {
            Assign(target, source, sizeof(ulong));
        }

        /// <summary>
        ///     Sets the default value to all elements of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains elements to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        public static void Clear<T>(this DiagonalMatrix<T> matrix)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            #endif

            Array.Clear(matrix.Values, 0, matrix.RowsCount);
        }

        /// <summary>
        ///     Sets the default value to all elements in the specified column of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the column to clear.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of the column to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="columnIndex"/> is negative or greater than or equal to columns count.
        /// </exception>
        public static void ClearColumn<T>(this DiagonalMatrix<T> matrix, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            #endif

            matrix.Values[columnIndex] = default(T);
        }

        /// <summary>
        ///     Sets the default value to all elements in the specified row of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the row to clear.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of the row to clear.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to rows count.
        /// </exception>
        public static void ClearRow<T>(this DiagonalMatrix<T> matrix, int rowIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            #endif

            matrix.Values[rowIndex] = default(T);
        }

        /// <summary>
        ///     Creates a deep copy of the matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="source">
        ///     The source matrix.
        /// </param>
        /// <returns>
        ///     A deep copy of the matrix.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="source"/> is <see langword="null"/>.
        /// </exception>
        public static DiagonalMatrix<T> Clone<T>(this DiagonalMatrix<T> source)
        {
            #if SAFE

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var result = new DiagonalMatrix<T>(source.RowsCount);
            result.Assign(source);
            return result;
        }

        /// <summary>
        ///     Gets the element in the matrix at the specified location.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the element to get.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of row of the element to get.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of column of the element to get.
        /// </param>
        /// <returns>
        ///     The element in the matrix at the specified location.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to
        ///     the number of rows of <paramref name="matrix"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="columnIndex"/> is negative or greater than or equal to
        ///         the number of columns of <paramref name="matrix"/>.
        ///     </para>
        /// </exception>
        public static T GetAt<T>(this DiagonalMatrix<T> matrix, int rowIndex, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            #endif

            return rowIndex != columnIndex ? default(T) : matrix.Values[rowIndex];
        }

        /// <summary>
        ///     Sets the element in the matrix at the specified location.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="matrix">
        ///     The matrix that contains the element to set.
        /// </param>
        /// <param name="newValue">
        ///     The new value for the element.
        /// </param>
        /// <param name="rowIndex">
        ///     The zero-based index of row of the element to set.
        /// </param>
        /// <param name="columnIndex">
        ///     The zero-based index of column of the element to set.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="matrix"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ElementIndexIsOutOfRangeException">
        ///     <paramref name="rowIndex"/> is negative or greater than or equal to
        ///     the number of rows of <paramref name="matrix"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="columnIndex"/> is negative or greater than or equal to
        ///         the number of columns of <paramref name="matrix"/>.
        ///     </para>
        /// </exception>
        /// <exception cref="ReadOnlyMatrixElementException">
        ///     <paramref name="rowIndex"/> and <paramref name="columnIndex"/> specifies the element
        ///     that is not in the portrait of <paramref name="matrix"/>.
        /// </exception>
        public static void SetAt<T>(this DiagonalMatrix<T> matrix, T newValue, int rowIndex, int columnIndex)
        {
            #if SAFE

            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if ((rowIndex < 0) || (matrix.RowsCount <= rowIndex))
            {
                throw new ElementIndexIsOutOfRangeException("rowIndex", rowIndex, 0, matrix.RowsCount - 1);
            }

            if ((columnIndex < 0) || (matrix.ColumnsCount <= columnIndex))
            {
                throw new ElementIndexIsOutOfRangeException("columnIndex", columnIndex, 0, matrix.ColumnsCount - 1);
            }

            if (rowIndex != columnIndex)
            {
                throw new ReadOnlyMatrixElementException("matrix", rowIndex, columnIndex);
            }

            #endif

            if (rowIndex != columnIndex)
            {
                return;
            }

            matrix.Values[rowIndex] = newValue;
        }

        #endregion

        #region private

        /// <summary>
        ///     Assigns the second matrix to the first matrix.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of values of matrix elements.
        /// </typeparam>
        /// <param name="target">
        ///     The matrix whose elements should be assigned.
        /// </param>
        /// <param name="source">
        ///     The matrix whose elements should be copied.
        /// </param>
        /// <param name="itemSize">
        ///     The size of one element of matrix in bytes.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="target"/> is <see langword="null"/>.
        ///     <para>
        ///         --or--
        ///     </para>
        ///     <para>
        ///         <paramref name="source"/> is <see langword="null"/>.
        ///     </para>
        /// </exception>
        /// <remarks>
        ///     The number of rows of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.RowsCount,<paramref name="source"/>.RowsCount).
        ///     The number of columns of <paramref name="source"/> to assign to <paramref name="target"/> is determined as
        ///     min(<paramref name="target"/>.ColumnsCount,<paramref name="source"/>.ColumnsCount).
        /// </remarks>
        private static void Assign<T>(DiagonalMatrix<T> target, DiagonalMatrix<T> source, int itemSize)
        {
            #if SAFE

            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            #endif

            var size = Math.Min(target.RowsCount, source.RowsCount);
            Buffer.BlockCopy(source.Values, 0, target.Values, 0, size * itemSize);
        }

        #endregion

        #endregion
    }
}